Skip to content

Commit b323e8b

Browse files
chore(zh): remove the references to images that is about to remove (#26941)
Co-authored-by: Jason Ren <40999116+jasonren0403@users.noreply.github.com>
1 parent 77332b3 commit b323e8b

File tree

10 files changed

+73
-267
lines changed
  • files
    • zh-cn
      • games/techniques/3d_on_the_web/building_up_a_basic_demo_with_three.js
      • learn_web_development
        • core
        • extensions/testing
        • getting_started/environment_setup/dealing_with_files
      • mdn/writing_guidelines/page_structures/code_examples
      • web/css/layout_cookbook/sticky_footers
    • zh-tw/learn_web_development
      • core/styling_basics/what_is_css
      • getting_started/environment_setup/dealing_with_files

10 files changed

+73
-267
lines changed

files/zh-cn/games/techniques/3d_on_the_web/building_up_a_basic_demo_with_three.js/index.md

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,28 @@ slug: Games/Techniques/3D_on_the_web/Building_up_a_basic_demo_with_Three.js
2525

2626
```html
2727
<!doctype html>
28-
<html>
28+
<html lang="zh-CN">
2929
<head>
30-
meta charset="utf-8">
31-
<title>MDN Games: Three.js demo</title>
30+
<meta charset="utf-8" />
31+
<title>MDN 游戏:Three.js 演示</title>
3232
<style>
33-
body {
33+
html,
34+
body,
35+
canvas {
3436
margin: 0;
3537
padding: 0;
36-
}
37-
canvas {
3838
width: 100%;
3939
height: 100%;
40+
font-size: 0;
4041
}
4142
</style>
4243
</head>
4344
<body>
44-
<script src="three.min.js"></script>
45+
<script src="https://cdn.jsdelivr.net/npm/three-js@79.0.0/three.min.js"></script>
4546
<script>
46-
var WIDTH = window.innerWidth;
47-
var HEIGHT = window.innerHeight;
48-
/* all our JavaScript code goes here */
47+
const WIDTH = window.innerWidth;
48+
const HEIGHT = window.innerHeight;
49+
/* 我们所有的 JavaScript 代码会出现在这里 */
4950
</script>
5051
</body>
5152
</html>
@@ -60,7 +61,7 @@ Before reading on, copy this code to a new text file, and save it in your workin
6061
A renderer is a tool that displays scenes right in your browser. There are a few different renderers: WebGL is the default one, and the others you can use are Canvas, SVG, CSS and DOM. They differ in a way everything is rendered, so the WebGL implementation will work differently than the CSS one, but the idea is to have it look exactly the same for the end user. Thanks to this approach, a fallback can be used if the primary technology is not supported by the browser.
6162

6263
```js
63-
var renderer = new THREE.WebGLRenderer({ antialias: true });
64+
const renderer = new THREE.WebGLRenderer({ antialias: true });
6465
renderer.setSize(WIDTH, HEIGHT);
6566
renderer.setClearColor(0xdddddd, 1);
6667
document.body.appendChild(renderer.domElement);
@@ -75,7 +76,7 @@ Add this code into the second {{htmlelement("script")}} element, just below the
7576
A scene is the place where everything happens. When creating new objects in the demo, we will be adding them all to the scene to make them visible on the screen. In three.js, the scene is reperesented by a `Scene` object. Let's create it, by adding the following line below our previous lines:
7677

7778
```js
78-
var scene = new THREE.Scene();
79+
const scene = new THREE.Scene();
7980
```
8081

8182
Later on we will be using the `.add()` method to add objects to the scene.
@@ -85,7 +86,7 @@ Later on we will be using the `.add()` method to add objects to the scene.
8586
我们有渲染场景,但是我们仍然需要一个摄像机来观察场景 - 想象没有摄像机的电影场景。下面的代码将摄像机放在三维坐标系中,并将其指向我们的场景,这样人们就能看到一些东西:
8687

8788
```js
88-
var camera = new THREE.PerspectiveCamera(70, WIDTH / HEIGHT);
89+
const camera = new THREE.PerspectiveCamera(70, WIDTH / HEIGHT);
8990
camera.position.z = 50;
9091
scene.add(camera);
9192
```
@@ -124,7 +125,7 @@ Again add the new code below your previous additions, then try saving the file a
124125
Now the scene is properly rendering we can start adding 3D shapes to it. To speed up development Three.js provides a bunch of predefined primitives that you can to create shapes instantly in a single line of code. There's cubes, spheres, cylinders and more complicated shapes available. Drawing the needed vertices and faces for given shape is taken care of by the framework, so we can focus on the high level coding. Let's start by defining the geometry for a cube shape — add the following just above the `render()` function:
125126

126127
```js
127-
var boxGeometry = new THREE.BoxGeometry(10, 10, 10);
128+
const boxGeometry = new THREE.BoxGeometry(10, 10, 10);
128129
```
129130

130131
In this case we define a simple cube that is 10 x 10 x 10 units. The geometry itself is not enough though — we also need a material that will be used for our shape.
@@ -134,7 +135,7 @@ In this case we define a simple cube that is 10 x 10 x 10 units. The geometry it
134135
Material is that thing covering the object — the colors or texture on its surface. In our case we will use a simple blue color to paint our box. There are predefined materials that can be used: Basic, Phong, Lambert. We will play with the last two later on, but for now the Basic one should be enough:
135136

136137
```js
137-
var basicMaterial = new THREE.MeshBasicMaterial({ color: 0x0095dd });
138+
const basicMaterial = new THREE.MeshBasicMaterial({ color: 0x0095dd });
138139
```
139140

140141
Add this line below the previous one.
@@ -146,7 +147,7 @@ Our material is ready, but what to do next?
146147
To apply the material to a geometry a mesh is used. It takes a shape and adds the specified material to every face:
147148

148149
```js
149-
var cube = new THREE.Mesh(boxGeometry, basicMaterial);
150+
const cube = new THREE.Mesh(boxGeometry, basicMaterial);
150151
```
151152

152153
Again, add this line below the previous one.
@@ -159,21 +160,54 @@ We've now created the actual cube using the geometry and material defined earlie
159160
scene.add(cube);
160161
```
161162

162-
If you save and refresh now, your object will look like a square, because it's facing the camera. The good thing about objects is that we can move them on the scene however we want, for example rotating and scaling as we like. Let's apply a little bit of rotation to the cube, so we can see more than one face — again, add below the previous one:
163+
## Three.js 形状演示
163164

164-
```js
165-
cube.rotation.set(0.4, 0.2, 0);
165+
如果你到目前为止按照所有步骤操作没有遇到任何问题,那么你已经使用 Three.js 在 3D 环境中创建了第一个对象!这比你想象的要简单,对吧?你的代码应该类似于下面的运行实例。你可以点击“Play”以在 MDN 代码演练场中查看和编辑代码:
166+
167+
```html hidden live-sample___three-js-intro
168+
<script src="https://cdn.jsdelivr.net/npm/three-js@79.0.0/three.min.js"></script>
166169
```
167170

168-
Congratulations, you've created your first object in a 3D environment! It was easier than you thought, right? Here's how it should look:
171+
```js hidden live-sample___three-js-intro
172+
const WIDTH = window.innerWidth;
173+
const HEIGHT = window.innerHeight;
169174

170-
![Blue cube on a gray background rendered with Three.js.](cube.png)
175+
const renderer = new THREE.WebGLRenderer({ antialias: true });
176+
renderer.setSize(WIDTH, HEIGHT);
177+
renderer.setClearColor(0xdddddd, 1);
178+
document.body.appendChild(renderer.domElement);
171179

172-
And here's the code we have created so far:
180+
const scene = new THREE.Scene();
173181

174-
{{JSFiddleEmbed("https://jsfiddle.net/end3r/bwup75fa/","","350")}}
182+
const camera = new THREE.PerspectiveCamera(70, WIDTH / HEIGHT);
183+
camera.position.z = 50;
184+
scene.add(camera);
175185

176-
You can also [check it out on GitHub](https://github.com/end3r/MDN-Games-3D/blob/gh-pages/Three.js/cube.html).
186+
const boxGeometry = new THREE.BoxGeometry(10, 10, 10);
187+
const basicMaterial = new THREE.MeshBasicMaterial({ color: 0x0095dd });
188+
const cube = new THREE.Mesh(boxGeometry, basicMaterial);
189+
scene.add(cube);
190+
cube.rotation.set(0.4, 0.2, 0);
191+
192+
function render() {
193+
requestAnimationFrame(render);
194+
renderer.render(scene, camera);
195+
}
196+
render();
197+
```
198+
199+
```css hidden live-sample___three-js-intro
200+
body,
201+
canvas {
202+
margin: 0;
203+
padding: 0;
204+
width: 100%;
205+
height: 100%;
206+
font-size: 0;
207+
}
208+
```
209+
210+
{{embedlivesample("three-js-intro", "", "400px")}}
177211

178212
## More shapes and materials
179213

@@ -186,9 +220,10 @@ cube.position.x = -25;
186220
Now onto the shapes and materials: what would you say for a torus using the Phong material? Try adding the following lines just below the lines that define the cube.
187221

188222
```js
189-
var torusGeometry = new THREE.TorusGeometry(7, 1, 6, 12);
190-
var phongMaterial = new THREE.MeshPhongMaterial({ color: 0xff9500 });
191-
var torus = new THREE.Mesh(torusGeometry, phongMaterial);
223+
const torusGeometry = new THREE.TorusGeometry(7, 1, 6, 12);
224+
const phongMaterial = new THREE.MeshPhongMaterial({ color: 0xff9500 });
225+
const torus = new THREE.Mesh(torusGeometry, phongMaterial);
226+
torus.rotation.set(0.5, 0.5, 0);
192227
scene.add(torus);
193228
```
194229

@@ -197,9 +232,9 @@ Thee lines will add a torus geometry; the `TorusGeometry()` method's parameters
197232
We can have even crazier predefined shapes; let's play some more — add the following lines below the ones that defined the torus:
198233

199234
```js
200-
var dodecahedronGeometry = new THREE.DodecahedronGeometry(7);
201-
var lambertMaterial = new THREE.MeshLambertMaterial({ color: 0xeaeff2 });
202-
var dodecahedron = new THREE.Mesh(dodecahedronGeometry, lambertMaterial);
235+
const dodecahedronGeometry = new THREE.DodecahedronGeometry(7);
236+
const lambertMaterial = new THREE.MeshLambertMaterial({ color: 0xeaeff2 });
237+
const dodecahedron = new THREE.Mesh(dodecahedronGeometry, lambertMaterial);
203238
dodecahedron.position.x = 25;
204239
scene.add(dodecahedron);
205240
```
@@ -213,15 +248,13 @@ As mentioned above, the new objects currently just look black. To have both the
213248
There are various types of light sources available in Three.js; the most basic one is the `PointLight`, which works like a flashlight — shinig a spotlight in a given direction. Add the following below your shapre definitions:
214249

215250
```js
216-
var light = new THREE.PointLight(0xffffff);
251+
const light = new THREE.PointLight(0xffffff);
217252
light.position.set(-10, 15, 50);
218253
scene.add(light);
219254
```
220255

221256
We define a white point of light, set it's position a bit away from the center of the scene so it can light up some parts of the shapes, and add it to the scene. Now everything works as it should — all three shapes are visible. You should check the documentation for other types of light like Ambient, Directional, Hemisphere or Spot, and experiment with placing them on the scene to see the effects.
222257

223-
![Shapes: blue cube, dark yellow torus and dark gray dodecahedron on a gray background rendered with Three.js.](shapes.png)
224-
225258
This looks a little bit boring though. In a game something is usually happening — we can see animations and such — so let's try to breathe a little life into those shapes by animating them.
226259

227260
## Animation
@@ -243,7 +276,7 @@ It will rotate the cube on every frame by a tiny bit, so it will look like a smo
243276
We can also scale a given object. By applying a constant value we could make it grow or shrink once, but let's make it more interesting. First, we will need a helper variable called `t` for counting the elapsed time. Add it right before the `render()` function:
244277

245278
```js
246-
var t = 0;
279+
let t = 0;
247280
```
248281

249282
Now let's increase the value by a given constant value on each frame of the animation; add the following lines just below the `requestAnimationFrame()` invocation:

files/zh-cn/learn_web_development/core/scripting/debugging_javascript/index.md

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,10 @@ if (window.XMLHttpRequest) {
8181

8282
[JSHint 主页](https://jshint.com/)提供了一个在线 linter,它可以让你在左侧输入 JavaScript 代码,并在右侧提供包含指标、警告和错误的输出。
8383

84-
![JSHint 屏幕截图。左边面板是一个有代码高亮并且显示行数的代码编辑器。右边面板为关于函数和警告的数量、大小和构成的指标。警告包括问题和所在行号。](jshint-online.png)
85-
8684
#### 代码编辑器插件
8785

8886
每次把代码复制并粘贴到网页上以检查其有效性并不方便,你真正需要的是能融入你的标准工作流程的 linter,而且麻烦最少。许多代码编辑器都有 linter 插件,例如,请参见 [JSHint 安装页面](https://jshint.com/install/)的“文本编辑器和 IDE 的插件”部分。
8987

90-
#### 其他方式
91-
92-
还有其他的方法来使用这种 linter;你可以在 [JSHint](https://jshint.com/install/)[ESLint](https://eslint.org/docs/user-guide/getting-started) 的安装页面上读到这些方法。
93-
94-
值得一提的是命令行的使用——你可以使用 npm(Node 包管理器——你必须先安装 [NodeJS](https://nodejs.org/zh-cn/))将这些工具作为命令行工具来安装(可通过 CLI——命令行界面)。例如,下面的命令安装了 JSHint:
95-
96-
```bash
97-
npm install -g jshint
98-
```
99-
100-
然后,你可以用这些工具处理你想 lint 的 JavaScript 文件,比如说:
101-
102-
![命令行输入了 jshint filename.js。响应是一个有行号的列表和对发现的错误的描述。](js-hint-commandline.png)
103-
104-
你也可以将这些工具与任务运行器/构建工具(如 [Gulp](https://gulpjs.com/)[Webpack](https://webpack.github.io/))一起使用,以便在开发过程中自动对你的 JavaScript 进行 lint。(见后面文章中的[使用任务运行器来自动测试工具](/zh-CN/docs/Learn_web_development/Extensions/Testing/Automated_testing#使用任务运行器作为自动化测试工具)。)关于 ESLint 选项,请参见 [ESLint 集成](https://eslint.org/docs/user-guide/integrations);Grunt 开箱即支持 JSHint,并且还有其他可用的集成,例如 [Webpack 的 JSHint 加载器](https://github.com/webpack-contrib/jshint-loader)
105-
106-
> [!NOTE]
107-
> 尽管 ESLint 的安装和配置过程比起 JSHint 更繁琐,但是它也更强大。
108-
10988
### 浏览器开发者工具
11089

11190
浏览器开发工具有许多有用的特性,可以帮助调试 JavaScript。首先,JavaScript 控制台会报告代码中的错误。
@@ -287,90 +266,6 @@ JavaScript 库往往有几个主要的种类(有些库包含其中的一个以
287266
> [!NOTE]
288267
> 你也会在 web 上遇到一些 JavaScript 框架,比如 [Ember](https://emberjs.com/)[Angular](https://angularjs.org/)。库通常可用于解决个别问题并放入现有网站中,而框架则更倾向于开发复杂 web 应用的完整解决方案。
289268
290-
#### Polyfill
291-
292-
Polyfill 也由第三方的 JavaScript 文件组成,你可以把它们放到你的项目中,但它们与库不同。库倾向于加强现有的功能,使一些需求可以更容易实现,而 Polyfill 提供的是根本不存在的功能。Polyfill 完全使用 JavaScript 或其他技术来建立对浏览器不支持的特性的支持。例如,你可以使用 [es6-promise](https://github.com/stefanpenner/es6-promise) 这样的 polyfill 来使 promise 在没有原生支持的浏览器中也能工作。
293-
294-
让我们一起来完成一个练习——在这个示例中,我们将使用 Fetch polyfill 和 es6-promise polyfill。虽然现代浏览器已经完全支持 Fetch 和 Promise,但如果我们的目标浏览器不支持 Fetch,那么这种浏览器很可能也不支持 Promise,因为 Fetch 依赖于 Promise。
295-
296-
1. 为了开始工作,在一个新的目录中复制我们的 [fetch-polyfill.html](https://github.com/mdn/learning-area/blob/main/tools-testing/cross-browser-testing/javascript/fetch-polyfill.html) 示例文件和[漂亮的鲜花图片](https://github.com/mdn/learning-area/blob/main/tools-testing/cross-browser-testing/javascript/flowers.jpg)。我们将编写代码来获取花的图片并在页面中显示。
297-
2. 接下来,在与 HTML 相同的目录中保存一份 [Fetch polyfill](https://raw.githubusercontent.com/github/fetch/master/fetch.js) 的副本。
298-
3. 使用以下代码将 polyfill 脚本应用到页面上,将这些脚本放在现有的 {{htmlelement("script")}} 元素上方,这样当我们开始尝试使用 Fetch 时,它们就已经在页面上可用了(我们还从 CDN 加载一个 Promise polyfill,因为 IE11 支持 promise,这是 fetch 所需要的):
299-
300-
```html
301-
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
302-
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
303-
<script src="fetch.js"></script>
304-
```
305-
306-
4. 在原来的 {{htmlelement("script")}} 元素内添加下列代码:
307-
308-
```js
309-
const myImage = document.querySelector(".my-image");
310-
311-
fetch("flowers.jpg").then((response) => {
312-
response.blob().then((myBlob) => {
313-
const objectURL = URL.createObjectURL(myBlob);
314-
myImage.src = objectURL;
315-
});
316-
});
317-
```
318-
319-
5. 即使你在不支持 [Fetch](/zh-CN/docs/Web/API/Window/fetch) 的浏览器中加载它,你仍然能够看到花的图像——这不是很酷吗?
320-
![一个 fetch 基本示例的标题,配一张紫色花朵的照片](fetch-image.jpg)
321-
322-
> [!NOTE]
323-
> 你可以在 [fetch-polyfill-finished.html](https://mdn.github.io/learning-area/tools-testing/cross-browser-testing/javascript/fetch-polyfill-finished.html) 找到我们的完成版(也请看[源代码](https://github.com/mdn/learning-area/blob/main/tools-testing/cross-browser-testing/javascript/fetch-polyfill-finished.html))。
324-
325-
> [!NOTE]
326-
> 在此重申,polyfill 有许多种方法可以利用——具体请查阅每个 polyfill 单独的文档。
327-
328-
你可能会问:“为什么我们要加载 polyfill 代码,即便在不需要的时候?”这是一个合理的疑问。随着网站变得更加复杂,开始引入更多的库和 polyfill,你可能会加载大量不必要的代码。这会影响网站性能,尤其是在性能较低的设备上。因此,只有在需要时才加载相应的文件是明智的。
329-
330-
要做到这一点,你需要在 JavaScript 代码中进行一些额外的配置。你需要进行特性检测,以确定浏览器是否支持你想要使用的特性:
331-
332-
```js
333-
if (browserSupportsAllFeatures()) {
334-
main();
335-
} else {
336-
loadScript("polyfills.js", main);
337-
}
338-
339-
function main(err) {
340-
// 主要的应用代码
341-
}
342-
```
343-
344-
因此,我们首先执行一个条件判断,检查 `browserSupportsAllFeatures()` 函数是否返回 `true`。如果返回 `true`,我们就执行 `main()` 函数(该函数包含我们应用程序的全部代码)。`browserSupportsAllFeatures()` 函数的实现可能如下所示:
345-
346-
```js
347-
function browserSupportsAllFeatures() {
348-
return window.Promise && window.fetch;
349-
}
350-
```
351-
352-
在这段代码中,我们检测浏览器是否支持 [`Promise`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise) 对象和 [`fetch()`](/zh-CN/docs/Web/API/Window/fetch) 函数。如果浏览器支持这两者,函数就会返回 `true`。如果函数返回 `false`,我们将执行条件语句的第二部分,调用名为 `loadScript()` 的函数来将 polyfill 加载到页面,然后执行 `main()` 函数。`loadScript()` 函数的实现可能是这样的:
353-
354-
```js
355-
function loadScript(src, done) {
356-
const js = document.createElement("script");
357-
js.src = src;
358-
js.onload = () => {
359-
done();
360-
};
361-
js.onerror = () => {
362-
done(new Error(`Failed to load script ${src}`));
363-
};
364-
document.head.appendChild(js);
365-
}
366-
```
367-
368-
此函数创建一个新的 `<script>` 元素,并设置其 `src` 属性为我们在上述代码中调用时指定的路径(`'polyfills.js'`)。脚本加载完成后,会执行我们第二个参数指定的函数(`main()`)。如果在加载脚本时发生错误,该函数仍会被调用,但会传入一个自定义错误,这样如果出现问题,我们可以通过这个错误来进行调试。
369-
370-
请注意,`polyfills.js` 实际上是将我们正在使用的两个 polyfill 合并到一个文件中。虽然我们是手动完成这一过程的,但也有更智能的工具可以自动为你生成捆绑文件——例如 [Browserify](https://browserify.org/)(你可以参考 [Browserify 基础教程](https://www.sitepoint.com/getting-started-browserify/)来了解如何开始使用)。将多个 JavaScript 文件捆绑在一起是一个好方法,它通过减少所需进行的 HTTP 请求数量来提升网站性能。
371-
372-
你可以在 [fetch-polyfill-only-when-needed.html](https://mdn.github.io/learning-area/tools-testing/cross-browser-testing/javascript/fetch-polyfill-only-when-needed.html) 查看这段代码的实际运行情况(也可查看[源代码](https://github.com/mdn/learning-area/blob/main/tools-testing/cross-browser-testing/javascript/fetch-polyfill-only-when-needed.html))。我们需要指出的是,这段代码的原创者是 Philip Walton。你可以阅读他的文章[只在需要时加载 Polyfill](https://philipwalton.com/articles/loading-polyfills-only-when-needed/),了解更多关于原始代码的信息,以及围绕这个主题的许多有用讨论。
373-
374269
#### 转译 JavaScript
375270

376271
对于那些想要使用现代 JavaScript 特性的开发者来说,另一个选择是将采用 ECMAScript 6/ECMAScript 2015 特性的代码转换成能够在旧版浏览器上运行的版本。

0 commit comments

Comments
 (0)